var app = getApp();
var constract = require('../../../utils/constract.js');
var ouid;
Page({
  data: {
    items: []
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);

    var that = this;
    var group_name = options.group_name ? options.group_name : '';
    var type = options.type ? options.type : '' ;
    var collection_uid;

    ouid = options.ouid ? options.ouid : '';
    
    if (options.id){
      collection_uid = options.id; 
    }else{
      collection_uid = app.globalData.ids
    }
    
    that.setData({
      collection_uid: collection_uid,
      type: type,
      group_name: group_name
    })
  },
  onShow: function(){
    var that = this;
    var group_name = that.data.group_name;
    app.Verification(function (token) {
      wx.request({
        url: constract.findGroup,
        header: { token: token },
        success: function (res) {
          console.log(res);

          var items = res.data.data;
          for (var i = 0; i < items.length; i++) {
            if (items[i].group_name == group_name) {
              items[i].checked = true;
            }
          }
          that.setData({
            items: items
          })
        }
      })
    })
  },
  radioChange: function(e){
    var group_id = null;
    var value = e.detail.value;
    var items = this.data.items;
    for(var i = 0; i < items.length; i++){
      if(items[i].checked){
        items[i].checked = false;
      }
      if(items[i].group_name==value){
        items[i].checked = true;
        group_id = items[i].id;
      }
    }
    this.setData({
      items: items,
      group_id: group_id
    })
    
  },
  sure: function(){
    var that = this;
    var collection_uid = this.data.collection_uid;
    var group_id = this.data.group_id;

    if (typeof collection_uid == "object"){ //批量操作
      app.Verification(function (token) {
        wx.request({
          method: 'POST',
          url: constract.batchGroup,
          header: {
            "content-type": "application/x-www-form-urlencoded",
            token: token
          },
          data: { cardId: collection_uid.cardIds, collectId: collection_uid.collectIds, group_id: group_id,collection_uid: collection_uid.ouids },
          success: function (res) {
            console.log(res)
            wx.showToast({
              title: '添加到分组成功',
              icon: 'success',
              mask: true,
              duration: 2000
            })
            setTimeout(function(){
              wx.navigateBack({
                delta: 2
              })
            },2000)
          }
        })
      })
    }else{
      app.Verification(function (token) {
        if(that.data.type == 0) {//扫描名片
          wx.request({
            url: constract.distribution,
            header: { token: token },
            data: { cardId: collection_uid, group_id: group_id },
            success: function (res) {
              wx.showToast({
                title: '修改分组成功',
                icon: 'success',
                mask: true,
                duration: 2000
              })
              setTimeout(function () {
                wx.navigateBack()
              }, 2000)
            }
          })
        } else {//名名用户
          wx.request({
            url: constract.distribution,
            header: { token: token },
            data: { collection_uid: ouid, group_id: group_id, myCard: collection_uid },
            success: function (res) {
              wx.showToast({
                title: '修改分组成功',
                icon: 'success',
                mask: true,
                duration: 2000
              })
              setTimeout(function () {
                wx.navigateBack()
              }, 2000)
            }
          })
        }
        
      })
    }
    
  }
})